stanfordfandomcom-20200214-history
Project 2: Django URLs, Views, and Templates
= Overview = Django is a Python framework that is easy to learn and suited for simple, practical designs of a database driven Web application. It comes with many facilities such as an object-relational mapper, an automatic administrator interface, an easy-to-use URL design feature, a powerful template system, a cache system, and support for international languages. For this project, we focused primarily on the front-end of Django, which includes a URL design feature, and views using the template system. = Framework Structure = Django stresses the idea of decoupling the many aspects of the framework. A philosophy similar to MVC is used for splitting the components of the framework. The basic components of Django are the model, URL, view and template. Views Using Django Template System As mentioned in class, a view generates a web page and can be thought of as the public interface of a page. In Django, each view is represented by a Python function which is called according to the URL received. An HttpRequest must be input to the view and and there is the option to add extra arguments. The view determines what will be displayed in the browser by first loading a template. The Django settings allow the programmer to specify search directories for templates, and Django will search through those directories sequentially until it finds the desired template, which is a html file. If the template is dynamic, which is usually the case, the view passes data called the 'context' into the template and converts the result into HTML. The view must return either an HttpResponse, which contains the html from the template used, or an exception like 404. URL Parsing In order to know when to call which view, the URL is parsed. One or many URL configuration files may exist for a given project. URLs are parsed as regular expressions which provides a wide range of possibilities for determining which views to call based on the URL. Template Features Templates are called by views to render html to display in the browser. A template can simply be an HTML file but its strength comes from the markup tags that can be placed in the file to perform simple looping, data displaying tasks. Inheritance By including {% extends "base.html" %} at the very top of a template, Django will use the blocks within the child template to fill in the blocks defined in the base template, thereby cutting down on redundancy. This is a very important feature of Django because it means that things like the the look and feel of a web page can be decoupled from the page's content. Instead of having to change the child template which has all the content, Django allows the programmer to just change how the base parent template displays the blocks, which will then be automatically filled with information from the child. This template system means the programmer can easily change how the site looks by changing the base.html file, without changing anything else. Template utilities: Substitution - Django will substitute anything within doubly curly braces with the actual value. Logical tags - Django allows you to use such constructs as if/else and for loops to conditionally display content. Filters - Django uses a Unix style pipe to allow you to filter values into a specified format. The programmer can use multiple filters and make custom filters too. Django Example - Web Survey To demonstrate how Django works we look at the example of an online survey with multiple choice questions. There are 2 models used in a survey: questions and answers. We have the choice of whether we would like to design the view or the URL first. For our survey we want the following pages: A title page from which we can begin the survey. A page displaying a form for the question and answer choices. A page displaying all the survey results. This amounts to 3 different views. We will let the index view process the title page, the question detail view display the question, and the result view display the survey results. Views are responsible for data processing and we need some code to store data from the forms on the detail view. To do this we create another vote view which stores the input into our models. We now need views to be called by specific URLs. We set up our URL configuration file which links URLs to views. If our website is http://www.survey.com, we can have http://www.survey.com/ load the index view. http://www.survey.com/polls/3 load the question view, which in this case will display the 3rd question. We can have http://www.survey.com/polls/2/vote/ call the vote view, which in this case will be for the 2nd question. Finally, we can have http://www.survey.com/results/ display the survey results. Key Notes: The question number is an input parameter but is implemented simply as part of the URL. Multiple URLs can call the same view. Both these issues will be discussed later on. = Unique Features = Built-in Admin Interface (SHOW SOME SCREEN SHOTS) Django allows the programmer to opt in to an automatically generated administrator interface for models. This feature is great because it reduces the amount of work for the programmer in specially creating these pages for an administrator role. The programmer enables this interface by adding an application to the INSTALLED_APPS setting, and the added administrator page has the automatic added path /admin. If the programmer had previously created a superuser and password, the administrator account defaults to those settings. Within this interface, the administrator can edit the content of the site, including changing the groups, users, and sites, which are the default Django features. For a model to be displayed on the admin page, however, Django requires the programmer to create an admin.py file and "register" the desired models. Once the model is on the admin page, it can be changed there without changing the code directly, which is extremely convenient. The admin interface is customizable. Although coding is required, methods and variables exist to achieve customizations with a few lines of code. Use of Regular Expressions in URL parsing Django enables the programmer to decouple URLs from their underlying functions by parsing the URL as a string rather than using it as a link. The URL is matched to a string pattern in the URL configuration file using regular expressions and is then matched it to a view. In comparison, in PHP the URL must correspond to the location of the code. In an earlier Python framework (CherryPy), the URL had to correspond to the name of the method used. These coupling rules cannot be maintained when the application reaches a high level of complexity. In Django, the Many interesting uses can be made of this design. First, as alluded to above, a view can be swapped, or the URL can be changed very easily in the URL configuration file because of the decoupling. Secondly, no "query" parameters are necessary in Django URLs, allowing for intuitive and elegant URLs. A typical html URL will contain ? and & to indicate the locations of parameters being passed in. We can indicate the parameter in our configuration file by matching it to a specific regular expression pattern. This makes URLs cleaner by eliminating the & and ? symbols as well as gives URLs logical meanings. For example, if we were making some sort of calculator we could have an URL like http://www.calculator.com/1/plus/5 that would return the result of 1+5=6. = Strengths = Django is an extremely simple and decoupled framework. Like we talked about in class, designing abstractions is the biggest issue in Computer Science. Django does an excellent job of abstracting between models and views, and the different components of a web application. Simplistically, using Django, a programmer can just write view functions and then map URLs to them using URLConf. Templates can also be used so that the logic of a page is separate from the appearance of a page, allowing different people to change different aspects of the application. The template system does not assume Python knowledge or experience, so that web page designers can easily write templates to change a page's appearance without having to be full fledged programmers, which is the right type of decoupling. Django's template system is also very flexible. Although its template language is very strong, and most people tend to use it, Django also allows the programmer to use a different template language very easily. Django's optional automatic admin interface is a huge strength, because it reduces the amount of coding on the programmer's behalf. Furthermore, the views and variables can all be adjusted in the admin interface, allowing the admin to manage all the acquired data without mucking with code. = Weaknesses = Because the template system is decoupled from the programming itself, the template cannot change the value of a variable, but can only render the variable's value. The programmer can write custom tags to accomplish this task, but the shipped tags in the system do not allow this. The template system also cannot call raw Python code, again, because it's intended to be decoupled from the actual programming. Custom tags can also be written to do this, but the functionality is not built into Django itself.